layui.use(['element','table','laytpl'],function () {
    var $ = layui.$,
        table = layui.table,
        layer = layui.layer;
    table.render({
        elem: '#demo'
        ,id:"mytable"
        ,url: '/advice/list' //数据接口
        ,page: true //开启分页
        ,limit:15
        ,limits:[15,30,60,100]
        ,response: {
            statusName: 'code' //数据状态的字段名称，默认：code
            ,statusCode: 200 //成功的状态码，默认：0
            ,msgName: 'msg' //状态信息的字段名称，默认：msg
            ,countName: 'total' //数据总数的字段名称，默认：count
            ,dataName: 'rows' //数据列表的字段名称，默认：data
        }
        ,cols: [[ //表头
            {type:'checkbox'}
            ,{title: '序号',type:'numbers'}
            ,{field: 'title', title: '名称'}

            ,{field: 'type', title: '类型',templet:function(r){
                switch (r.type){
                    case 1: return "民政部门通知";
                    case 2: return "养老机构通知";
                }
            }}
            ,{field: 'createdTime', title: '创建时间',templet:function(r){
                    return $.dateFormat(new Date(r.createdTime),'yyyy-MM-dd HH:mm:ss');
               }}

            ,{title:'操作',toolbar:"#toolbar",width:300}

        ]]
    });


    //工具条
    table.on("tool(test)",function(obj){
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        if("show" === layEvent){
            ii =  layer.open({
                type: 2,
                content: '/advice/show?id='+data.id,
                area: ['850px', '650px'],
                title:'通知详情',
            });
        } else if("edit" === layEvent){
            location.href = "/advice/edit?id=" + data.id;
        }else if("delete" === layEvent){
            location.href = "/advice/delete?id=" + data.id;
        }
    });

    //批量删除
    $("#deleteAll").on('click',function(){
        var checkStatus = layui.table.checkStatus('mytable'); //test即为基础参数id对应的值
        if(checkStatus.data.length < 1) return false;
        var params = '';
        $.each(checkStatus.data,function(i,v){
            params += "id=" + v.id + "&";
        });
        //删除
        location.href = "/advice/delete?" + params;
    });

    //发布通知
    $("#publish").on('click',function(){
        location.href = "/advice/publish" ;
    });
});